package structure;

import java.util.NoSuchElementException;

public class PriorityQ {

    private int maxSize = 10;
    private int[] elements;
    private int count;

    public PriorityQ() {
        elements = new int[maxSize];
        count = 0;
    }
    
    public PriorityQ(int capacity) {
        maxSize = capacity;
        elements = new int[maxSize];
        count = 0;
    }
    
    public void insert(int element) {
        if (count > maxSize) {
            throw new IndexOutOfBoundsException("PriorityQ overflow");
        }
        int j;
        if (count == 0) {
            elements[count++] = element;
        } else {
            for (j = count - 1; j >= 0; j--) {
                if (element > elements[j]) {
                    elements[j + 1] = elements[j];
                } else {
                    break;
                }
            }
            elements[j + 1] = element;
            count++;
        }
    }
    
    public int peek() {
        if (isEmpty()) {
            throw new NoSuchElementException("PriorityQ underflow");
        }
        
        return elements[--count];
    }
    
    public void delete() {
        if (isEmpty()) {
            throw new NoSuchElementException("PriorityQ underflow");
        }
        count--;
    }
    
    public boolean isEmpty() {
        return count == 0;
    }
}
